﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using bmapi.HelperClass;
using bmapi.Models.DataEntity;
using static bmapi.Models.ShowModel;

namespace bmapi.Controllers
{
    [Route("lsh")]
    public class LSHController : BuyiController
    {
        // GET: api/LSH
        /// <summary>
        /// 根据流水号获取信息
        /// </summary>
        /// <param name="lsh"></param>
        public HttpResponseMessage Get(int lsh)
        {
            string sql = "select LSH_tb.name as UserName,LSH_tb.pod as Pod,Order_tb.name as OrderName,Order_tb.creater_time as Time,Order_tb.state as State from LSH_tb join Order_tb on LSH_tb.order_id = Order_tb.id where LSH_tb.LSH = @lsh";
            SQLCommand sc = new SQLCommand(sql, "@lsh", lsh);
            DataTable dt = sc.ExecuteScalars();
            if (dt == null)
                return AssistFun.toJson(new { status = 201, tip = "没有相关信息" });
            else
            {
                List<ShowLSH> lshList = AssistFun.DataTableToList<ShowLSH>(dt);
                return AssistFun.toJson(new { status = 200, tip = "成功", data = new { total = lshList.Count, rows = lshList } });
            }
        }

        [HttpGet,Route("downloadLSH")]
        public HttpResponseMessage DownLoad(string OrderName,int Pod,int State,string token)
        {
            
            //对应的生产单已经完成  在历史目录中找pod文件
            string podPath = "";
            if (State == 2)
            {
                podPath = m_buyiRoot + "\\buyiHistory\\" + OrderName + "\\pod\\" + OrderName + "[" + Pod + "]";
                return DownPod(podPath);
            }
            else
            {
                podPath = m_buyiRoot + "\\buyiWork\\" + OrderName + "\\pod\\" + OrderName + "[" + Pod + "]";
                return DownPod(podPath);
            }
        }

        public HttpResponseMessage DownPod(string podPath)
        {
            if (File.Exists(podPath + ".pod"))
            {
                Download(podPath);
                return AssistFun.toJson(new { status = 200, tip = "成功" });
            }

            else if (File.Exists(podPath + "#.pod"))
            {
                Download(podPath);
                return AssistFun.toJson(new { status = 200, tip = "成功" });
            }
            else
                return AssistFun.toJson(new { status = 201, tip = "文件不存在," + podPath });
        }

    }
}
